function setCookie(name, val) {
    var exdate = new Date(),
        expiredays = 30;
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(val) + ";expires=" + exdate.toGMTString();
}


function getCookie(name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=")
        if (c_start != -1) {
            c_start = c_start + name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

function deleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        return 1;
    }

}

/**
 * 格式化卡号 每四位添加一个空格
 * @param cardNum [16 digits]
 * @return cardNum after formatted
 */
function cardNumFormat(cardNum) {
    if (/\S{5}/.test(cardNum)) {
        var formated = cardNum.replace(/(.{4})/g, "$1 ");
        return formated;
    }
}

/**
 * 格式化时间，返回：年、月、日、时、分、秒、毫秒、季度
 * @return {[type]}
 */
function formatDate(d) {
    var date;
    if (d) {
        date = new Date(d);
    } else {
        date = new Date();
    }
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var ss = date.getMilliseconds();
    var q = Math.floor((M + 3) / 3);
    var day = date.getDay();
    M = M > 0 && M < 10 ? "0" + M : M;
    d = d > 0 && d < 10 ? "0" + d : d;
    h = h >= 0 && h < 10 ? "0" + h : h;
    m = m >= 0 && m < 10 ? "0" + m : m;
    s = s >= 0 && s < 10 ? "0" + s : s;
    if (ss > 0 && ss < 10) {
        ss = "00" + ss;
    } else if (ss >= 10 && ss <= 100) {
        ss = "0" + ss;
    }
    return [y, M, d, h, m, s, ss, q, day];

}

/**
 *
 * @return {[type]}
 */
function thisWeek(d) {
    var date;
    if (d) {
        date = new Date(d);
    } else {
        date = new Date();
    }
    date.setDate(date.getDate() - 7);
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var ss = date.getMilliseconds();
    var q = Math.floor((M + 3) / 3);
    M = M > 0 && M < 10 ? "0" + M : M;
    d = d > 0 && d < 10 ? "0" + d : d;
    h = h >= 0 && h < 10 ? "0" + h : h;
    m = m >= 0 && m < 10 ? "0" + m : m;
    s = s >= 0 && s < 10 ? "0" + s : s;
    if (ss > 0 && ss < 10) {
        ss = "00" + ss;
    } else if (ss >= 10 && ss <= 100) {
        ss = "0" + ss;
    }
    return [y, M, d, h, m, s, ss, q];

}
/**
 * 获取URL中含有目标参数（name）的值
 * @param name
 * @returns {null}
 */
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

/**
 * 字符串字节数
 * @param name
 * @returns {len}
 */
function strlen(str){
    var len = 0;
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        }
        else {
            len+=2;
        }
    }
    return len;
}

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null){
        return  unescape(r[2]);
    }
    return null;
}